package com.chatplus.application.service.basedata.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chatplus.application.aiprocessor.constant.AIConstants;
import com.chatplus.application.common.constant.GroupCacheNames;
import com.chatplus.application.common.logging.SouthernQuietLogger;
import com.chatplus.application.common.logging.SouthernQuietLoggerFactory;
import com.chatplus.application.common.util.CacheGroupUtils;
import com.chatplus.application.common.util.PlusJsonUtils;
import com.chatplus.application.dao.basedata.ConfigDao;
import com.chatplus.application.domain.dto.AdminConfigDto;
import com.chatplus.application.domain.dto.ChatConfigDto;
import com.chatplus.application.domain.dto.NoticeConfigDto;
import com.chatplus.application.domain.entity.basedata.ConfigEntity;
import com.chatplus.application.enumeration.AiPlatformEnum;
import com.chatplus.application.file.dto.FileConfigDto;
import com.chatplus.application.service.basedata.ConfigService;
import com.thoughtworks.xstream.InitializationException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 第三方AI配置表业务逻辑实现
 *
 * <p>Table: t_config - 第三方AI配置表</p>
 *
 * @author developer
 * @see ConfigEntity
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigDao, ConfigEntity> implements ConfigService {
    private static final SouthernQuietLogger LOGGER = SouthernQuietLoggerFactory.getLogger(ConfigServiceImpl.class);

    @Override
    public void init() {
        CacheGroupUtils.clear(GroupCacheNames.SYS_AI_SETTING);
        LOGGER.message("开始加载Chat Plus 系统配置到缓存").info();
        List<ConfigEntity> list = this.list();
        if (list == null || list.isEmpty()) {
            throw new InitializationException("加载Chat Plus 系统配置到缓存失败");
        }
        list.forEach(configEntity -> {
            String marker = configEntity.getMarker();
            String value = configEntity.getConfig();
            switch (marker) {
                case "chat":
                    ChatConfigDto chatConfigDto = PlusJsonUtils.parseObject(value, ChatConfigDto.class);
                    if (chatConfigDto == null) {
                        LOGGER.message("chatConfigDto加载AI参数配置失败").error();
                    } else {
                        // 总的缓存
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_CHAT_REDIS_KEY, chatConfigDto);
                        // 以下为细分的缓存
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AiPlatformEnum.AZURE, chatConfigDto.getAzure());
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AiPlatformEnum.BAI_DU, chatConfigDto.getBaidu());
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AiPlatformEnum.CHAT_GLM, chatConfigDto.getChatGml());
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AiPlatformEnum.OPEN_AI, chatConfigDto.getOpenAi());
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AiPlatformEnum.XUN_FEI, chatConfigDto.getXunFei());
                    }
                    break;
                case "system":
                    AdminConfigDto adminConfigDto = PlusJsonUtils.parseObject(value, AdminConfigDto.class);
                    if (adminConfigDto == null) {
                        LOGGER.message("adminConfigDto加载系统参数配置失败").error();
                    } else {
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_CONFIG_REDIS_KEY, adminConfigDto);
                    }
                    break;
                case "notice":
                    NoticeConfigDto noticeConfigDto = PlusJsonUtils.parseObject(value, NoticeConfigDto.class);
                    if (noticeConfigDto == null) {
                        LOGGER.message("noticeConfigDto加载系统参数配置失败").error();
                    } else {
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_NOTICE_REDIS_KEY, noticeConfigDto);
                    }
                    break;
                case "file":
                    FileConfigDto fileConfigDto = PlusJsonUtils.parseObject(value, FileConfigDto.class);
                    if (fileConfigDto == null) {
                        LOGGER.message("fileConfigDto加载系统参数配置失败").error();
                    } else {
                        CacheGroupUtils.put(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_FILE_REDIS_KEY, fileConfigDto);
                    }
                    break;
                default:
                    LOGGER.message("未知配置KEY").context("marker", marker).warn();
                    break;
            }
        });
        LOGGER.message("结束加载Chat Plus 系统配置到缓存").info();
    }

    @Override
    public ConfigEntity getByMarker(String marker) {
        return this.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getMarker, marker).last("limit 1"));
    }

    @Override
    public AdminConfigDto getSystemConfig() {
        return CacheGroupUtils.get(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_CONFIG_REDIS_KEY);

    }

    @Override
    public ChatConfigDto getChatConfig() {
        return CacheGroupUtils.get(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_CHAT_REDIS_KEY);
    }

    @Override
    public NoticeConfigDto getNoticeConfig() {
        return CacheGroupUtils.get(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_NOTICE_REDIS_KEY);
    }

    @Override
    public FileConfigDto getFileConfig() {
        return CacheGroupUtils.get(GroupCacheNames.SYS_AI_SETTING, AIConstants.SYSTEM_FILE_REDIS_KEY);
    }
}
